package com.vcredit.creditcat.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.model.Feedback;
import com.vcredit.creditcat.vo.FeedbackVo;
import com.vcredit.creditcat.framework.pagination.Criteria;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.persistence.base.FeedbackMapper;

 /*
  * @author zhangxingong
  * @version 1.0
  * @since 1.0
 */
@Service
public class FeedbackService {
    
  @Autowired
	private FeedbackMapper feedbackMapper;
    
	public Page<Feedback> findByPage(Criteria criteria){

		List  list=feedbackMapper.findByPage(criteria);
		List list2=new ArrayList();
		if(null!=list&&list.size()>0){
			for(int i=0;i<list.size();i++){
				Feedback  feedback=(Feedback) list.get(i);
				feedback=changer(feedback);
				list2.add(feedback);
			}
		}
		
	    return new Page<Feedback>(feedbackMapper.findByPageCount(criteria), list2);
     }
	
	
	private Feedback changer(Feedback feedback){
		
		String mes1=feedback.getFbContent();
		String mes2="<a href=\"javascript:void(0);\" onclick=\"js_method(event,'%s')\" >%s</a>";
		String mes3=String.format(mes2, mes1, mes1);
		feedback.setFbContent(mes3);		
		
		
		String mes4=feedback.getFbDesc();
		String mes5="<a href=\"javascript:void(0);\" onclick=\"js_method(event,'%s')\" >%s</a>";
		String mes6=String.format(mes5, mes4, mes4);
		
		feedback.setFbDesc(mes6);	
		return feedback;
	}
	
	public List<Feedback> find(Feedback feedback){
        List  list=feedbackMapper.find(feedback);
		List list2=new ArrayList();
		if(null!=list&&list.size()>0){
			for(int i=0;i<list.size();i++){
				Feedback  feedback_l=(Feedback) list.get(i);
				feedback_l=changer(feedback_l);
				list2.add(feedback_l);
			}
		}	
	    return list2;
	}
	
	public Feedback findById(Long feedbackId){
	    return change(feedbackMapper.findById(feedbackId));
	}
	
	public List<Feedback> findAll(){	
	    return feedbackMapper.findAll();
	}
	
	

	 
	private Feedback change(Feedback feedback){
		     String mes=feedback.getFbContent();		     		     		     
		     String kk="<div class=\"div1\"><span>%s</span></div>";			     
		     feedback.setFbContent(String.format(kk,mes,mes));
		     return feedback;
	}
	
	public void save(FeedbackVo feedbackVo){
	  try {
	     Feedback feedback = new Feedback();
	     BeanUtils.copyProperties(feedback, feedbackVo);	     
	     feedbackMapper.insert(feedback);
       }catch (Exception e) {
           e.printStackTrace();
       }
	}
	
	public void update(Feedback feedback){
		feedbackMapper.update(feedback);
	}
	
	public void removeFeedback(String ids){
	  for (String feedbackIdString : ids.split(",")) {
        Long feedbackId = Long.parseLong(feedbackIdString);
        feedbackMapper.delete(feedbackId);
      }
	}
	
}
